<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">

      <el-form-item label="收费站" prop="deptId" v-if="deptId==101">
        <el-tree-select v-model="queryParams.deptId" :data="deptOptions" :props="{ value: 'id', label: 'label', children: 'children' }" value-key="id"
          class="margin-right" placeholder="请选择部门" check-strictly clearable @change="bindChangeTree($event,'deptId')"  />

      </el-form-item>
      <el-form-item label="时间" prop="dateSection">
        <el-date-picker v-model="queryParams.dateSection" value-format="YYYY-MM-DD" type="daterange" range-separator="-" start-placeholder="开始日期"
        end-placeholder="结束日期" @change="bindChangeQueryAllDate"></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['audit:seminar:add']">新增</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['audit:seminar:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="seminarList" @selection-change="handleSelectionChange" show-overflow-tooltip>
      <el-table-column label="序号" align="center" type="index" width="60" />
      <el-table-column label="收费站" align="center" prop="deptName" />
      <el-table-column label="开展时间" align="center" prop="seminarDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.seminarDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="上报时间" align="center" prop="reportDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.reportDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column> -->
      <el-table-column label="上报人" align="center" prop="reporterName" />
      <el-table-column label="签到表" align="center" prop="signUrl">
        <template #default="scope">
          <el-button @click.stop="previewFun(scope.row.signUrl)" v-if="scope.row.signUrl">查看</el-button>
          <span v-else>暂无数据</span>
        </template>
      </el-table-column>
      <el-table-column label="会议纪要" align="center" prop="meetingUrl">
        <template #default="scope">
          <el-button @click.stop="previewFun(scope.row.meetingUrl)" v-if="scope.row.meetingUrl">查看</el-button>
          <!-- <image-preview :src="scope.row.meetingUrl" :width="50" :height="50" v-if="scope.row.meetingUrl" /> -->
          <span v-else>暂无数据</span>
        </template>
      </el-table-column>
      <el-table-column label="照片" align="center" prop="imgUrl">
        <template #default="scope">
          <image-preview :src="scope.row.imgUrl" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['audit:seminar:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['audit:seminar:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改研讨会对话框 -->
    <el-dialog :title="title" v-model="open" width="1000px" append-to-body>

      <el-form ref="seminarRef" :model="form" :rules="rules" label-width="80px">

        <el-row :gutter="15">
          <el-col :span="12">
            <el-form-item label="收费站" prop="deptId">
              <el-tree-select style="width: 100%;" v-model="form.deptId" :data="deptOptions" :props="{ value: 'id', label: 'label', children: 'children' }"
                value-key="id" placeholder="请选择" check-strictly />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="开展时间" prop="seminarDate">
              <el-date-picker style="width: 100%;" clearable v-model="form.seminarDate" type="date" value-format="YYYY-MM-DD" placeholder="请选择开展时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="签到表" prop="signUrl">
              <FileUpload :modelValue="form.signUrl" v-model="form.signUrl" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="会议纪要" prop="meetingUrl">
              <FileUpload :modelValue="form.meetingUrl" v-model="form.meetingUrl" />
              <!-- <ImageUpload :modelValue="form.meetingUrl" v-model="form.meetingUrl" /> -->
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="照片" prop="imgUrl">
              <ImageUpload :modelValue="form.imgUrl" v-model="form.imgUrl" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入备注" :rows="5" />
            </el-form-item>
          </el-col>
        </el-row>

      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <Viewer ref="fileViewerRef" :dialog-doc="dialogDoc" />
  </div>
</template>

<script setup name="Seminar">
import moment from 'moment'
import { deptTreeSelect } from "@/api/system/user";
import ImageUpload from "@/components/ImageUpload"
import FileUpload from "@/components/FileUpload"
import Viewer from "@/components/Custom/viewer.vue";
import useUserStore from '@/store/modules/user'
import { listSeminar, getSeminar, delSeminar, addSeminar, updateSeminar } from "@/api/charge/seminar";

const { proxy } = getCurrentInstance();
const userStore = useUserStore()
const deptOptions = ref([])
const seminarList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const dialogDoc = ref(false)
const fileViewerRef = ref(null);
const deptId = ref()

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    startDate:'',
    endDate:""
  },
  rules: {
  }
});

const { queryParams, form, rules } = toRefs(data);

function getDeptTree() {
  deptTreeSelect().then(response => {
    deptOptions.value = response.data;
  });
};


/** 查询研讨会列表 */
function getList() {
  loading.value = true;
  listSeminar(queryParams.value).then(response => {
    seminarList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {};
  proxy.resetForm("seminarRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();

  title.value = "添加研讨会";
  form.value.deptId = deptId.value;
  open.value = true;
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();

  const _id = row.id || ids.value
  getSeminar(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改研讨会";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["seminarRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateSeminar(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addSeminar(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除研讨会编号为"' + _ids + '"的数据项？').then(function () {
    return delSeminar(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => { });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('audit/seminar/export', {
    ...queryParams.value
  }, `研讨会_${new Date().getTime()}.xlsx`)
}
const bindChangeTree = (val, name) => {
  queryParams.value[name] = val;
}
// 预览
const previewFun = (file) => {
  const baseUrl = import.meta.env.VITE_APP_BASE_API;
  dialogDoc.value = true;
  let data = {};
  // 上传过的文件组成完成的网络路径
  if (file&&file.indexOf(',')==-1) {
    data.src = `${baseUrl}${file}`;
  }else if(file&&file.indexOf(',')!=-1){
    let src=file.split(',');
    let srcArr=[]
    src.forEach(item=>{srcArr.push(`${baseUrl}${item}`)})
    data.src = srcArr.join(',');
    file=srcArr[0]
  }

  const arr = file.split(".");
  const suffix = arr[arr.length - 1];
  if (suffix == "docx" || suffix == "doc") {
    fileViewerRef.value?.viewDocx(data,'研讨会');
  } else if (suffix == "xlsx" || suffix == "xls") {
    fileViewerRef.value?.viewXlsx(data);
  } else if (suffix == "jpg" || suffix == "jpeg" || suffix == "png") {
    fileViewerRef.value?.viewImg(data);
  } else if (suffix == "pdf") {
    fileViewerRef.value?.viewPdf(data);
  }
};
const bindChangeQueryAllDate = (val) => {
  queryParams.value.dateSection = [moment(val[0]).format("YYYY-MM-DD"), moment(val[1]).format("YYYY-MM-DD")]
  queryParams.value.startDate = queryParams.value.dateSection[0];
  queryParams.value.endDate = queryParams.value.dateSection[1];
}
const getDeptId = () => {
  userStore.getInfo().then(res => {
    if (res.code == 200) {
      deptId.value = res.user.dept.deptId;
      queryParams.value.deptId=deptId.value;
      getList()
    }

  })
}
getDeptTree()
getDeptId()
</script>

<style scoped>
.align-center {
  display: flex;
  align-items: center;
}
</style>
